package com.woniu.adapter;


import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.outlet.dao.MaterialBomPoMapper;
import com.woniu.outlet.po.MaterialBomPo;
import com.woniu.outlet.rabbitmq.MessageRepo;
import com.woniu.outlet.redis.RedisRepo;
import com.woniu.utils.SnowflakeIdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class MaterialBomAdapter {

    @Autowired
    private MaterialBomPoMapper materialBomPoMapper;

    @Autowired
    private RedisRepo redisRepo;

    @Autowired
    private MessageRepo messageRepo;


    public List<MaterialBomPo> findMaterialBoms() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String str = redisRepo.findRedisRepo("materialBom:list");
        List<MaterialBomPo> lists = new ArrayList<>();
        if (!StringUtils.isEmpty(str)) {
            lists = mapper.readValue(str, new TypeReference<List<MaterialBomPo>>() {
            });
            return lists;
        }
        lists = materialBomPoMapper.selectByExample(null);
        //存到redis
        redisRepo.saveRedisRepo("materialBom:list",mapper.writeValueAsString(lists));
        return lists;
    }

    public MaterialBomPo getMaterialBomById(Long id) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String str = redisRepo.findRedisRepo("materialBom:" + id);
        MaterialBomPo materialBomPo = null;
        if (!StringUtils.isEmpty(str)) {
            materialBomPo= mapper.readValue(str, new TypeReference<MaterialBomPo>() {
            });
            return materialBomPo;
        }
        materialBomPo= materialBomPoMapper.selectByPrimaryKey(id);
        //存到redis
        redisRepo.saveRedisRepo("materialBom:"+id,mapper.writeValueAsString(materialBomPo));
        return materialBomPo;
    }


    public MaterialBomPo getMaterialBomByMaterialCode(String materialCode) throws Exception  {

        ObjectMapper mapper = new ObjectMapper();
        String str = redisRepo.findRedisRepo("materialBom:" + materialCode);
        MaterialBomPo materialBomPo = null;
        if (!StringUtils.isEmpty(str)) {
            materialBomPo= mapper.readValue(str, new TypeReference<MaterialBomPo>() {
            });
            return materialBomPo;
        }
        materialBomPo= materialBomPoMapper.selectByMaterialCode(materialCode);
        //存到redis
        redisRepo.saveRedisRepo("materialBom:"+materialCode,mapper.writeValueAsString(materialBomPo));
        return materialBomPo;
    }


    public void addMaterialBom(MaterialBomPo materialBomPo){
        SnowflakeIdGenerator snowflakeIdGenerator = new SnowflakeIdGenerator();
        long id = snowflakeIdGenerator.nextId();
        materialBomPo.setMaterialCode("WLBM"+id);
        materialBomPoMapper.insert(materialBomPo);
        redisRepo.deleteRedisRepo("materialBom:list");

    }

    public void updateMaterialBom(MaterialBomPo materialBomPo){
        materialBomPoMapper.updateByPrimaryKeySelective(materialBomPo);
        redisRepo.deleteRedisRepo("materialBom:list");
    }

    public void deleteMaterialBomById(Long id){
        materialBomPoMapper.deleteByPrimaryKey(id);
        redisRepo.deleteRedisRepo("materialBom:list");
    }


}
